/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.MachineManagement.Components;

import Beans.Requests.MediumManagement.MediumManagementRequest;
import Utilities.Constants.StorageDeviceAction;
import Utilities.PermissionsConstants;
import java.util.ArrayList;

/**
 * This class represents the storage settings of a virtual machine.
 *
 * @author Angel Sanadinov
 */
public class StorageSettings extends MachineManagementComponent
{
    private ArrayList<MediumManagementRequest> mediaRequests;   //any request for the media
    private ArrayList<DeviceSettings> devicesSettings;          //settings for the devices/media
    private ArrayList<ControllerSettings> controllersSettings;  //new controllers settings
    private ArrayList<String> controllersForRemoval;            //controllers to be removed

    /**
     * Constructs an incomplete storage settings object. <br><br>
     *
     * <b>Note:</b> <i>All storage settings data needs to be explicitly set!</i>
     */
    public StorageSettings()
    {
        this.mediaRequests = new ArrayList<MediumManagementRequest>();
        this.devicesSettings = new ArrayList<DeviceSettings>();
        this.controllersSettings = new ArrayList<ControllerSettings>();
        this.controllersForRemoval = new ArrayList<String>();
    }

    /**
     * Sends a request for a new medium which will be attached to the machine,
     * based on the supplied data.
     *
     * @param deviceSettings attachment settings
     * @param mediumRequestData management request for the new medium
     */
    public void attachNewMedium(DeviceSettings deviceSettings, MediumManagementRequest mediumRequestData)
    {
        deviceSettings.setDeviceAction(StorageDeviceAction.CREATE);
        devicesSettings.add(deviceSettings);
        mediaRequests.add(mediumRequestData);
    }

    /**
     * An existing medium will be attached to the machine, based on the supplied
     * data.
     *
     * @param deviceSettings attachment settings
     */
    public void attachExistingMedium(DeviceSettings deviceSettings)
    {
        deviceSettings.setDeviceAction(StorageDeviceAction.ATTACH);
        devicesSettings.add(deviceSettings);
        mediaRequests.add(null);
    }

    /**
     * An existing medium will be detached from the machine, based on the supplied
     * data.
     *
     * @param deviceSettings detachment settings
     */
    public void detachExistingMedium(DeviceSettings deviceSettings)
    {
        deviceSettings.setDeviceAction(StorageDeviceAction.DETACH);
        devicesSettings.add(deviceSettings);
        mediaRequests.add(null);
    }

    /**
     * An existing medium will replace another medium on the machine, based
     * on the supplied data. <br><br>
     *
     * It is the same as separately attaching and detaching media.
     *
     * @param deviceSettings attachment settings
     */
    public void replaceWithExistingMedium(DeviceSettings deviceSettings)
    {
        deviceSettings.setDeviceAction(StorageDeviceAction.REPLACE);
        devicesSettings.add(deviceSettings);
        mediaRequests.add(null);
    }

    /**
     * Adds a new storage controller for the virtual machine.
     *
     * @param controllerSettings controller settings
     */
    public void addStorageController(ControllerSettings controllerSettings)
    {
        controllersSettings.add(controllerSettings);
    }

    /**
     * Removes an existing storage controller from the virtual machine. <br><br>
     *
     * <b>Note:</b> <i>Only valid for existing virtual machines.</i>
     *
     * @param controllerName the name of the controller to be removed
     */
    public void removeStorageController(String controllerName)
    {
        controllersForRemoval.add(controllerName);
    }

    /**
     * Retrieves all media requests for the storage devices.
     *
     * @return a list of all available media requests
     */
    public ArrayList<MediumManagementRequest> getMediaRequests()
    {
        return mediaRequests;
    }

    /**
     * Retrieves all storage devices settings.
     *
     * @return a list of all available devices settings
     */
    public ArrayList<DeviceSettings> getDevicesSettings()
    {
        return devicesSettings;
    }

    /**
     * Retrieves all controllers settings.
     *
     * @return a list of all available controllers settings
     */
    public ArrayList<ControllerSettings> getControllersSettings()
    {
        return controllersSettings;
    }

    /**
     * Retrieves the names of all controllers that are to be removed.
     *
     * @return a list of controllers names for removal
     */
    public ArrayList<String> getControllersForRemoval()
    {
        return controllersForRemoval;
    }

    /**
     * Checks if the supplied request data is valid.
     *
     * @return <code>true</code> if the data is valid or <code>false</code> if it
     *         is not
     */
    private boolean isRequestsDataValid()
    {
        boolean result = false;

        if(mediaRequests.size() == devicesSettings.size())
        {
            for(int i = 0; i < mediaRequests.size(); i++)
            {
                DeviceSettings device = devicesSettings.get(i);
                MediumManagementRequest request = mediaRequests.get(i);

                //checks that each device creation actions has a valid
                //hard disk creation request associated with it
                if(device.getDeviceAction() == StorageDeviceAction.CREATE
                        && (request == null
                            || (request.getRequestedAction() != PermissionsConstants.ACTION_MEDIUM_CREATE_HDD
                            && request.getRequestedAction() != PermissionsConstants.ACTION_MEDIUM_CREATE_HDD_DIFF)))
                    return false;
                else if(device.getDeviceAction() != StorageDeviceAction.CREATE && request != null)
                    return false;
                else
                    ;
            }

            result = true;
        }
        else
            ;

        return result;
    }

    @Override
    public boolean isValid()
    {
        if(mediaRequests != null && devicesSettings != null && controllersSettings != null
                && controllersForRemoval != null && isRequestsDataValid())
        {
            for(MediumManagementRequest request : mediaRequests)
            {
                if(request != null && !request.isValid())
                    return false;
                else
                    ;
            }

            return true;
        }
        else
            return false;
    }
}
